import {UserModel} from '../../../models/user.js'
import {updateUserBackUrl} from "../../../config";
//获取应用实例
const app = getApp()
const userModel = new UserModel()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    TabCur: 0,
    scrollLeft:0,
    tabList:['约拍动态','作品相册'],
    defaultUrl:'',
    imageList:[],
    nickname: '',
    shootList: [],
    userId: 0,
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60,
    })
    const tab = e.currentTarget.dataset.id
    if (this._beforeLoad(tab).length == 0) {
      wx.showLoading({
        title: '正在加载中...',
      })
      tab == 0?this._loadShootList(0):this._loadWorksImageList(0);
    }
  },
  _beforeLoad(TabCur) {
    switch (TabCur) {
      case 0:
        return this.data.shootList
      case 1:
        return this.data.imageList
      default:
        return []
    }
  },
  //图片点击事件
  imgPreview:function(event){
    let src = event.currentTarget.dataset.src;//获取data-src
    let imgList = event.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  ChooseImage() {
    let _this = this
    wx.chooseImage({
      count:1,
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        _this.uploadImage(res.tempFilePaths)
      }
    });
  },
  uploadImage(path) {
    let _this = this
    let url = updateUserBackUrl
    wx.showLoading({
      title: '正在上传中...',
    })
    wx.uploadFile({
      url: url, //仅为示例，非真实的接口地址
      filePath: path[0],
      header:{
        'token': wx.getStorageSync('token')
      },
      name: 'file',
      success (res){
        wx.hideLoading()
        console.log(res)
        _this.setData({
          defaultUrl:path
        })
      }
    })

  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../center/index?userId='+this.data.userId
    })
  },
  bindToWorks: function(event) {
    wx.navigateTo({
      url: '../works/index?userId='+this.data.userId+'&nickname='+this.data.nickname
    })
  },
  bindToFollow: function() {
    if (this.data.userId != 0) {
      return
    }
    wx.navigateTo({
      url: '../follow/index?userId='+this.data.userId
    });
  },
  bindToFans: function() {
    if (this.data.userId != 0) {
      return
    }
    wx.navigateTo({
      url: '../fans/index?userId='+this.data.userId
    })
  },
  onLoad: function (options) {
    this._setOptions(options)
    wx.showLoading({
      title: '正在加载中...',
    })
    this._init(this.data.userId)
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  _init:function(userId) {
    this._loadUserInfo(userId)
    const tab = this.data.TabCur
    tab == 0?this._loadShootList(0):this._loadWorksImageList(0)
  },

  _loadUserInfo(userId) {
    userModel.getUserIndex(userId).then(res=>{
      this.setData({
        userInfo: res.data,
        hasUserInfo: true,
        defaultUrl: res.data.backgroundUrl == null?'/images/home/1.jpg':res.data.backgroundUrl
      })
    })
  },
  _loadShootList(start,moreLoad) {
    wx.showLoading({
      title: '正在加载中...',
    })
    userModel.getUserShootList(start,this.data.userId).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          shootList: res.data,
        })
      }else {
        this.setData({
          shootList: app._LoadMore(this.data.shootList,res.data),
        })
        this.unLoading(res.data)
      }
      wx.hideLoading()
    })
  },
  _loadWorksImageList(start,moreLoad) {
    wx.showLoading({
      title: '正在加载中...',
    })
    userModel.getWorksImageList(start,this.data.userId).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          imageList: res.data,
        })
      }else {
        this.setData({
          imageList: app._LoadMore(this.data.imageList,res.data),
        })
        this.unLoading(res.data)
      }
      wx.hideLoading()
    })
  },
  onReady: function() {
    let nickname = this.data.nickname
    // 页面首次渲染完毕时执行
    if (typeof (nickname) != 'undefined' && nickname!='') {
      wx.setNavigationBarTitle({
        title: nickname+'的主页'
      });
    }
  },
  _setOptions:function (options) {
    if (options.userId != null) {
      this.setData({
        userId:options.userId,
        nickname:options.nickname
      })
    }
  },
  handlerOnView: function (event) {
    let id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/index/details/about/index?id='+id
    })
  },
  //下拉刷新
  onPullDownRefresh:function() {
    let _this = this
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function()
    {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      _this._init(_this.data.userId)
    },1500);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const tab = this.data.TabCur
    tab == 0?this._loadShootList(this.data.shootList.length,true):
        this._loadWorksImageList(this.data.imageList.length,true)
  },
  unLoading(data) {
    if (data.length == 0) {
      app.showInfo('没有更多了哦~')
    }
  },
})
